package com.controller;

import com.model.*;
import com.response.Response;
import com.service.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/dashboard")
public class DashboardController {
    
    @Resource
    private StudentsService studentsService;
    
    @Resource
    private NoticesService noticesService;
    
    @Resource
    private WorksService worksService;
    
    @Resource
    private OrdersService ordersService;
    
    // 获取仪表盘统计数据
    @RequestMapping(value = "/stats")
    @ResponseBody
    @CrossOrigin
    public Response getStats(HttpServletRequest req) throws Exception {
        try {
            Map<String, Object> stats = new HashMap<>();
            
            // 获取用户数量
            Students students = new Students();
            int userCount = studentsService.getCount(students);
            stats.put("userCount", userCount);
            
            // 获取资讯数量
            Notices notices = new Notices();
            int newsCount = noticesService.getCount(notices);
            stats.put("newsCount", newsCount);
            
            // 获取任务数量
            Works works = new Works();
            int taskCount = worksService.getCount(works);
            stats.put("taskCount", taskCount);
            
            // 获取订单数量
            Orders orders = new Orders();
            int orderCount = ordersService.getCount(orders);
            stats.put("orderCount", orderCount);
            
            return Response.success(stats);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error(500, "服务器错误: " + e.getMessage());
        }
    }
}
